/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 *
 *           File:  bsc.h
 *    Description:  Binary simmetric channel SystemC model
 *       
 *        Created:  15.02.2013 15:57:29
 *         Author:  Aleksey Golovchenko (lexgolovchenko@gmail.com)
 *   Organization:  St.Petersburg State Polytecnical Universisy
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "systemc.h"

#define RANDU ((double)rand()/RAND_MAX)

/*
 * Binary Simmetric Channel (BSC) SystemC module
 */
SC_MODULE(bsc) 
{
    SC_HAS_PROCESS(bsc);

    // ------- Input ports -------------------    
    sc_in<sc_uint<10> > inp;

    // ------- Output ports ------------------
    sc_out<sc_uint<10> > out;

    // ------- Internal signals & variables --
    sc_uint<10> m_err;
    const double m_p;

    bsc(const char *name, double p) : sc_module(sc_module_name(name)), 
                                      m_p(p) 
    {
        SC_METHOD(transfer);
        sensitive << inp;          
    }

    // ------- Transfered signal -------------
    void transfer() 
    {                       
        
        // generate error vector
        for (int i = 0; i < 10; ++i) {
            m_err[i] = (RANDU < m_p) ? 1 : 0;
        }

        // add error to output signal
        out.write(inp.read() ^ m_err);
    }
};

